var util = require('../../utils/util.js')
Page({
  data: {
    imgUrls: [

    ],
    indicatorDots: true,
    autoplay: true,
    interval: 10000,
    duration: 500,
    act: '',
    userInfo: '',


  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var _this = this;
    //检测用户是否登录
    util.userLogin(function() {
      //用户登录成功后回调

      wx.getStorage({
        key: 'userInfo',
        success: function(res) {
          _this.setData({
            userInfo: res.data
          });
        }
      })
      _this.findInfo();

    });

  },
  /**
   * 查询首页信息
   */
  findInfo: function() {
    var _this = this;
    util.request('/wxapi/helpActivity/queryCompanyInfo', {}, function(data) {

      var introduce = JSON.parse(data.rows[0].extValue).introduce;
      console.log(introduce);
      var acts = data.rows[0].actList;
      _this.setData({
        imgUrls: introduce.split(","),
      })

      if (acts) {
        _this.setData({
          act: acts[0],
        })
      }

    }, true);



  },
  onShareAppMessage: function(res) {
    var _this = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '我在免费领取' + _this.data.act.hacName+'邀你来助力',
      path: 'pages/active/active?id=' + _this.data.act.actId + '&openId=' + _this.data.userInfo.openId,
      imageUrl: _this.data.act.hacRemark,
      success: function(res) {
        // 转发成功
        _this.shareSuccess();

      },

    }
  },
  /**
   * 分享成功
   */
  shareSuccess: function() {
    var _this = this;
    util.request('/wxapi/helpActivity/addShare/' + _this.data.act.actId, {}, function(data) {

    }, true);
  },
  /**
   * 获取二维码
   */
  getQrcode: function (formId) {
    var _this = this;
    if(_this.data.qrcode){
      wx.previewImage({
        current: _this.data.qrcode, // 当前显示图片的http链接
        urls: [_this.data.qrcode] // 需要预览的图片http链接列表
      })
    }else{

      util.request('/wxapi/helpActivity/getwxacode1/' + _this.data.act.actId + '/' + formId, 
      {}, function (data2) {
        var qrcode = data2.rows[0].codeRemark
        _this.setData({
          qrcode: qrcode,
        })

        wx.previewImage({
          current: _this.data.qrcode, // 当前显示图片的http链接
          urls: [_this.data.qrcode] // 需要预览的图片http链接列表
        })

      }, true);


    }
   

  },


  /**
    * 分享
    */
  formSubmit: function (e) {
    console.log('领取礼品：', e)
    var formId = e.detail.formId;
    this.getQrcode(formId)
  },
})